package com.dao;



import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import java.sql.*;


/**
 * 基础服务，获取connection，以及关闭connection
 * @author zyf
 */
public class BaseDao {

    private String propsPath;

    private static DruidDataSource dataSource;

    public BaseDao(String propsPath) {
        this.propsPath = propsPath;
        dataSource = generateDataSource();
    }


    /**
     * 返回DriverManager创建的connection
     * @return
     */
    public Connection getConnection() {
        Configuration config = null;
        try {
            config = new PropertiesConfiguration(this.propsPath);
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
        // access configuration properties
        String driverName = config.getString("database.driverClassName");
        String dbUrl = config.getString("database.url");
        String dbUser = config.getString("database.user","root");
        String dbPass = config.getString("database.password", "root");
//        long dbTimeout = config.getLong("database.timeout");
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        // 打开连接，连接数据库
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(dbUrl, dbUser,dbPass);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 返回数据库连接池druid的Connection
     * @return
     */
    public DruidPooledConnection getConnectionFromDruid() {
        DruidPooledConnection conn = null;
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    private DruidDataSource generateDataSource() {
        Configuration config = null;
        try {
            config = new PropertiesConfiguration(this.propsPath);
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
        // access configuration properties
        String driverName = config.getString("database.driverClassName");
        String dbUrl = config.getString("database.url");
        String dbUser = config.getString("database.user","root");
        String dbPass = config.getString("database.password", "root");
        dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverName);
        dataSource.setUrl(dbUrl);
        dataSource.setUsername(dbUser);
        dataSource.setPassword(dbPass);
        dataSource.setMaxActive(20);
        dataSource.setKeepAlive(true);
        return dataSource;
    }

    public static void closeAll(Connection conn, Statement stmt, ResultSet rs) throws SQLException {
        if(rs!=null) {
            rs.close();
        }
        if(stmt!=null) {
            stmt.close();
        }
        if(conn!=null) {
            conn.close();
        }
    }

    public static void closeAll(Connection conn, Statement stmt) throws SQLException {
        if(stmt!=null) {
            stmt.close();
        }
        if(conn!=null) {
            conn.close();
        }
    }

    public String getPropsPath() {
        return this.propsPath;
    }

    public void setPropsPath(String propsPath) {
        this.propsPath = propsPath;
    }
}
